﻿using Autofac.Core;
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using Castle.Core.Logging;

namespace PlutoStudio.Dependency
{
    internal class PropertySelector : IPropertySelector
    {
        public bool InjectProperty(PropertyInfo propertyInfo, object instance)
        {
            if (propertyInfo.AttributeExists<IgnoreAutowiredAttribute>())
                return false;
            if (propertyInfo.PropertyType == typeof(ILogger))
                if (propertyInfo.GetIndexParameters().Length == 0)
                    if (propertyInfo.GetAccessors().Length != 1 || propertyInfo.GetAccessors()[0].ReturnType == typeof(void))
                    {
                        var logger = IocManager.Instance.Resolve<ILoggerFactory>().Create(instance.GetType());
                        propertyInfo.SetValue(instance, logger, null);
                        return false;
                    }
            return true;
        }
    }
}
